﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BabyLearnEnglish_srcum_demo
{
    public partial class frmChoi : Form
    {
        int curQNumber = 0;
        int curA = 0;
        int corect = 0;
        int[] arrA = new int[100];
        question Answer = new question();

        public frmChoi()
        {
            InitializeComponent();
        }

        private void frmChoi_Load(object sender, EventArgs e)
        {
            curQNumber = 0;
            curA = 0;
            corect = 0;
            arrA = new int[100];
            getQandA();
        }

        public void getQandA()
        {
            Random random;
            int randomNumber;
            for (int i = 0; i < 4; i++)
            {
                int k = i;
                random = new Random();
                randomNumber = random.Next(0, Form1.listQ.Count);
                int pos = Array.IndexOf(arrA, randomNumber);
                if (pos == -1 && Form1.listQ[randomNumber].Result != Form1.listCurQ[curQNumber].Result)
                    arrA[i] = randomNumber;
                else
                    i = k-1;
            }

            rdDapDan1.Text = Form1.listQ[arrA[0]].Result;
            rdDapDan1.Tag = Form1.listQ[arrA[0]];

            rdDapDan2.Text = Form1.listQ[arrA[1]].Result;
            rdDapDan2.Tag = Form1.listQ[arrA[1]];

            rdDapDan3.Text = Form1.listQ[arrA[2]].Result;
            rdDapDan3.Tag = Form1.listQ[arrA[2]];

            rdDapDan4.Text = Form1.listQ[arrA[3]].Result;
            rdDapDan4.Tag = Form1.listQ[arrA[3]];

            random = new Random();
            randomNumber = random.Next(0, 4);
            arrA[randomNumber] = curQNumber * 100;
            curA = randomNumber;
            switch (curA)
            {
                case 0:
                    rdDapDan1.Text = Form1.listCurQ[curQNumber].Result;
                    rdDapDan1.Tag = Form1.listCurQ[curQNumber];
                    break;
                case 1:
                    rdDapDan2.Text = Form1.listCurQ[curQNumber].Result;
                    rdDapDan2.Tag = Form1.listCurQ[curQNumber];
                    break;
                case 2:
                    rdDapDan3.Text = Form1.listCurQ[curQNumber].Result;
                    rdDapDan3.Tag = Form1.listCurQ[curQNumber];
                    break;
                case 3:
                    rdDapDan4.Text = Form1.listCurQ[curQNumber].Result;
                    rdDapDan4.Tag = Form1.listCurQ[curQNumber];
                    break;
                default:
                    break;
            }

            Image image = Image.FromFile("../../data/img/" + Form1.listCurQ[curQNumber].Url);
            pictureBox.Image = image;
        }

        private void btnTiepTuc_Click(object sender, EventArgs e)
        {
            Progress();

            label1.Text = Form1.score.ToString();
            if (curQNumber == Form1.listCurQ.Count - 2)
                btnTiepTuc.Text = "Hoàn tất";
            else if (curQNumber == Form1.listCurQ.Count - 1)
            {
                frmHoanThanh frm = new frmHoanThanh();
                this.Hide();
                frm.ShowDialog();
                this.Show();
                this.Close();
            }
            if (curQNumber <= Form1.listCurQ.Count - 2)
            {
                curQNumber++;
                getQandA();
            }
        }

        private void btnDungLai_Click(object sender, EventArgs e)
        {

        }

        public void Progress()
        {
            if (Answer.Result == Form1.listCurQ[curQNumber].Result)
            {
                corect++;
                Form1.score += Form1.listCurQ[curQNumber].Score;
            }
            rdDapDan1.Checked = true;
        }

        private void rdDapDan1_CheckedChanged(object sender, EventArgs e)
        {
            Answer = (question)rdDapDan1.Tag;
        }

        private void rdDapDan2_CheckedChanged(object sender, EventArgs e)
        {
            Answer = (question)rdDapDan2.Tag;
        }

        private void rdDapDan3_CheckedChanged(object sender, EventArgs e)
        {
            Answer = (question)rdDapDan3.Tag;
        }

        private void rdDapDan4_CheckedChanged(object sender, EventArgs e)
        {
            Answer = (question)rdDapDan4.Tag;
        }
    }
}
